<?php


namespace app\Utils;


use Alipay\EasySDK\Kernel\Config;
use Alipay\EasySDK\Kernel\Factory;
use Exception;

class AliPayUtil
{
    protected $app_id;
    protected $private_key;
    protected $public_key;
    protected $key;
    protected $out_trade_no;
    protected $trade_no;
    protected $body;
    protected $notify_url;
    protected $total_fee;
    protected $buyer_id;

    public function __construct($app_id,$private_key,$public_key, $key,$options = [])
    {
        $this->app_id       = $app_id;
        $this->private_key  = $private_key;
        $this->public_key   = $public_key;
        $this->key          = $key;
        $this->out_trade_no = !empty($options['out_trade_no']) ? $options['out_trade_no'] : "";
        $this->trade_no     = !empty($options['trade_no']) ? $options['trade_no'] : "";
        $this->body         = !empty($options['body']) ? $options['body'] : "";
        $this->notify_url   = !empty($options['notify_url']) ? $options['notify_url'] : "";
        $this->total_fee    = !empty($options['total_fee']) ? $options['total_fee'] : 0;
        $this->buyer_id     = !empty($options['buyer_id']) ? $options['buyer_id'] : "";
    }

    /**
     * 统一下单接口
     * @return mixed
     */
    public function payOrder()
    {
        //1. 设置参数（全局只需设置一次）
        $options = new Config();
        $options->protocol           = 'https';
        $options->gatewayHost        = 'openapi.alipay.com';
        $options->signType           = 'RSA2';
        $options->appId              = $this->app_id;
        //应用私钥
        $options->merchantPrivateKey = $this->private_key;
        //支付宝公钥
        $options->alipayPublicKey    = $this->public_key;
        //可设置异步通知接收服务地址（可选）
        $options->notifyUrl          = $this->notify_url;
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        $options->encryptKey         = $this->key;
        Factory::setOptions($options);
        try {
            //2. 发起API调用（以支付能力下的统一收单交易创建接口为例）
            $result = Factory::payment()->app()->pay($this->body, $this->out_trade_no, $this->total_fee);
            //$result = Factory::payment()->common()->create($this->body, $this->out_trade_no, $this->total_fee, $this->buyer_id);
            //3. 处理响应或异常
            return ['code'=>0,'data'=>$result->body,'msg'=>"调用成功". PHP_EOL];
            /*if (!empty($result->code) && $result->code == 10000) {
                return ['code'=>0,'data'=>$result,'msg'=>"调用成功". PHP_EOL];
            } else {
                return ['code'=>-1,'data'=>null,'msg'=>"调用失败，原因：". $result->msg."，".$result->sub_msg];
            }*/
        } catch (Exception $e) {
            return ['code'=>-1,'data'=>null,'msg'=>"调用失败，". $e->getMessage()];
        }
    }

    /**
     * 退款接口
     * @return array
     */
    public function refundOrder()
    {
        //1. 设置参数（全局只需设置一次）
        $options = new Config();
        $options->protocol           = 'https';
        $options->gatewayHost        = 'openapi.alipay.com';
        $options->signType           = 'RSA2';
        $options->appId              = $this->app_id;
        //应用私钥
        $options->merchantPrivateKey = $this->private_key;
        //支付宝公钥
        $options->alipayPublicKey    = $this->public_key;
        //可设置异步通知接收服务地址（可选）
        $options->notifyUrl          = $this->notify_url;
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        $options->encryptKey         = $this->key;
        Factory::setOptions($options);
        try {
            //2. 发起API调用 refund（交易退款）
            $result = Factory::payment()->common()->refund($this->trade_no, $this->total_fee);
            //3. 处理响应或异常
            return ['code'=>0,'data'=>$result,'msg'=>"调用成功". PHP_EOL];
            /*if (!empty($result->code) && $result->code == 10000) {
                return ['code'=>0,'data'=>$result,'msg'=>"调用成功". PHP_EOL];
            } else {
                return ['code'=>-1,'data'=>null,'msg'=>"调用失败，原因：". $result->msg."，".$result->sub_msg];
            }*/
        } catch (Exception $e) {
            return ['code'=>-1,'data'=>null,'msg'=>"调用失败，". $e->getMessage()];
        }

    }

}